package cn.lentme.app.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import cn.lentme.app.base.BaseActivity;
import cn.lentme.app.component.utils.HttpUtil;
import cn.lentme.app.consts.ConstPool;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class PaperReadActivity extends BaseActivity {

    private TextView nameText, contentText;
    private Button btnReadMore;

    private Handler handler = new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paper_read);
        initView();
    }

    private void initView(){
        nameText = findViewById(R.id.paper_read_name);
        contentText = findViewById(R.id.paper_read_content);
        btnReadMore = findViewById(R.id.btn_read_more);

        btnReadMore.setOnClickListener(view -> requestPaper());

        requestPaper();
    }

    private void requestPaper(){
        new Thread(()->{
            String name = null;
            String content = null;

            OkHttpClient client = HttpUtil.client;
            Request request = new Request.Builder()
                    .url(ConstPool.PaperServerURL + "/pop_letter")
                    .build();
            try(Response response = client.newCall(request).execute()) {
                boolean successFlag = false;
                String toaskString = null;
                if(response.code()==200){
                    String bodyString = response.body().string();
                    if(bodyString.equals("null")){
                        contentText.setVisibility(View.GONE);
                        toaskString = "没有更多纸条了哦！";
                    }
                    else{
                        JSONObject json = JSONObject.parseObject(bodyString);
                        name = json.getString("userName");
                        content = json.getString("content");
                        successFlag = true;
                    }
                }
                else{
                    toaskString = "code: " + response.code() + ", " + response.message();
                }

                if(successFlag){
                    final String name_text = name;
                    final String content_text = content;
                    handler.post(()->{
                        nameText.setText(name_text);
                        contentText.setText(content_text);
                        contentText.setVisibility(View.VISIBLE);
                    });
                }
                else{
                    final String showString = toaskString;
                    handler.post(()->{
                        showSnackbar(contentText, showString);
                        contentText.setText("");
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
